package mypackage;

import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
	private byte[] record;
	
	private RecordThread recordThread;
	private PlayThread playThread;
	
    /**
     * Creates a new MyScreen object
     */
    public MyScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("MyTitle");
        
        recordThread = new RecordThread();
        
        //start record
        ButtonField recButton = new ButtonField("record");
        recButton.setRunnable(new Runnable() 
        {
			public void run() 
			{
				recordThread.start();
			}
		});
        add(recButton);
        
        //finish record
        ButtonField stopButton = new ButtonField("stop");
        stopButton.setRunnable(new Runnable() 
        {
			public void run() 
			{
				record = recordThread.stop();
				Log.d("stop button pressed", "record byte length:" + record.length);
			}
		});
        add(stopButton);
        
        //start play
        ButtonField playButton = new ButtonField("player");
        playButton.setRunnable(new Runnable() 
        {
			public void run() 
			{
				if(0 != record.length)
				{
					playThread = new PlayThread(record);
					playThread.start();
					
				}
				else 
				{
					Dialog.alert("record faild");
				}
			}
        });
        add(playButton);
    }
}
